package com.yuan.yblog.portal.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

/**
 * @author hepeiyuan on 2024/5/21 23:14
 * @description lua脚本配置
 */
@Configuration
public class RedisLuaScript {

    /**
     * 滑动窗口限流 reids-lua 脚本
     * @return redis脚本对象
     */
    @Bean
    public DefaultRedisScript<Long> SlidingWindowLimitScript() {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/SlidingWindow.lua")));
        redisScript.setResultType(Long.class);
        return redisScript;
    }
}
